<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "java.util.*, java.lang.*, java.net.*, java.io.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Twitter Conversation Retrieval</title>
	<link rel="stylesheet" href="general.css" type="text/css" />
</head>
<body>
	<span class="rtop">
	<span class="r1"></span>
	<span class="r2"></span>
	<span class="r3"></span>
	<span class="r4"></span>
	</span>

<div id="search_div">
	<div id="title">Twitter Conversation Retrieval</div>
	<br/>
		
	<div id="result_div">
		<a id="return_back" href="javascript:history.back()">Return to results <img src="return.jpg"/></a>
		<div id="result_title">Conversation:</div>
	</div>
	
	<%
			
	String server = "<specify ConversationServer IP Address>";
	int port = <specify CRConversationServer port>;

	String id = request.getParameter("id");

	// CONNECTION
	
	BufferedReader in = null;
	PrintStream serverout = null;
	//DataOutputStream os = null;
	Socket socket = null;
		
	boolean error = false;

		try {
			socket = new Socket(server, port);
			in = new BufferedReader( new InputStreamReader(socket.getInputStream()) );
			serverout = new PrintStream(socket.getOutputStream(), true);
		}
		catch (Exception e) { 
			error = true;
			out.println("Cannot connect to Search Server");
		}
			
		String search;

		ArrayList<String[]> l = new ArrayList<String[]>();

		if (!error) {
		try {
			serverout.println(id);
			while ((search = in.readLine())!=null) {
				if (search.trim().equals("")) continue;
				String[] entry = search.split("\t");
				// 0: ID 1: TIME 2: NAME 3: SCREEN_NAME 4: IMAGE 5: FOLLOWERS 6: TEXT 7: REPLY_TO
				l.add(entry);
		    	}
		    	
		    	out.print("<div id=\"discussion_div\">");
		    	
		    	getReply2("-1", l, 1, false, out);
		    	
		    	out.print("</div>");
		} catch (Exception e) {
			out.println("Cannot retrieve conversation: " + e.getMessage());
		}
		}
	%>
	
	<%!
	public void getReply2(String id, ArrayList<String[]> l, int spaces, boolean off, JspWriter out) throws IOException
	{
		boolean off_b = off;
		/* Partiamo dal primo elemento e ricostruiamo la conversazione */
	    for(int i=0;i<l.size();i++) {
		    	String[] r = l.get(i);
			if(r[7].equals(id)) {				
				if ( spaces != 1 ) {
					out.print("<div class=\"reply\">");
				}
				
				String id_r = r[0];
				
				out.print("<img class=\"avatar\" src=\""+r[4]+"\" />");
				out.print("<span class=\"right\">");
				out.print("<span class=\"title_tweet_discussion\">"+createURL(r[6])+"</span>");
				out.print("<br/>");
				out.print("<span class=\"date_tweet\">"+r[1]+"</span>");
				out.print(" - "); 
				out.print("<span class=\"author_tweet\">"+r[2]+"</span>");
				out.print("</span>");
				
				getReply2(id_r, l, spaces+1, off_b, out);
				
				if( spaces != 1 ) {
					out.print("</div>");
				}
			}
		}
	}
	%>
	
	<%!
	public String createURL(String text)
	{
		/* Inserisce link url */
		text = text.replaceAll("(.*)(http://([^<>[:space:]]+[[:alnum:]/?]*))(.*)", "$1<a href=\"$2\" target=\"_blank\">$2</a>$4");
		
		return text;
	}
	%>


	<br/><br/>
	</div>

	<span class="rbottom">
	<span class="r4"></span>
	<span class="r3"></span>
	<span class="r2"></span>
	<span class="r1"></span>
	</span>
</body>
</html>
